package com.base.system.managedBean;

import java.io.IOException;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.user.util.Browser;

import com.base.security.SecurityConst;
import com.base.security.entity.SecurityRole;
import com.base.system.managedBean.baseBean.BaseSecurityRoleBean;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_editSecurityRoleBean", scope = ManagedBeanScope.REQUEST)
public class EditSecurityRoleBean extends BaseSecurityRoleBean {
	Log logger = LogFactory.getLog(EditSecurityRoleBean.class);
	@Inject
	FacesContext context;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			Map<String, Object> session = context.getExternalContext()
					.getSessionMap();
			securityRole = (SecurityRole) session
					.get(SecurityConst.SECURITYROLE_SELECT_ROW_DATA);
			if (SecurityConst.ROLETYPE_SYSTEM
					.equals(securityRole.getRoleType())) {
				try {
					context.getExternalContext().getRequestMap().put(
							SecurityConst.WARNING_INFO, "系统级角色不能编辑！");
					context.getExternalContext().dispatch(
							SecurityConst.LOCATION_WARNING);
					return;
				} catch (IOException e) {
					e.printStackTrace();
					return;
				}
			}
			this.getSecurityRoleProperty();

		}
	}

	@Action(id = "saveSecurityRole")
	public void saveSecurityRole() {
		securityRole = securityEntityManager.getRoleById(Integer.parseInt(id
				.getValue().toString()));
		setSecurityRoleProperty();
		securityEntityManager.saveRole(securityRole);
		String script = "closeCenterLayoutTab('"
				+ SecurityConst.SECURITYROLE_EDIT_TAB + "')";
		Browser.execClientScript("window.top", script);
	}
}
